## 
/*
Project: 		The Impact of Short-Term Employment for Low-Income Youth: Experimental 	
				Evidence from the Philippines
Authors: 		Emily A. Beam and Stella Quimbo

***********************************
Path: 			code/02_analysis
File name: 		table_a07.do 
***********************************

Purpose: 		Generates balance table 

Inputs: 		spes_data.dta
Outputs: 		table_a07.xlsx					*balance table 
				beamquimbo_tables/Table A7		*t-test and RI p-values 

Log: 			results/03_output/log_table_a07.log		*Contains joint test p-value

Last updated: 	18 November 2021
*/ 


loc filename "beamquimbo_tables"


cap log close
log using "$output/log_table_a07.log",replace

** Reps for RI
loc reps = 1000

#delimit ; 


use "$usedata_analysis/spes_data",clear ;

* Identify covariates for balance tests ;

global balance "_bb_female  _bb_age _bb_edulvl _bb_workany _bb_workformal _bb_worki _bb_la _bb_expected_earn _bb_expt _bb_othexp";


keep _bb_female _bb_edulvl _bb_age  _bb_workany _bb_workformal _bb_worki _bb_la _bb_expected_earn _bb_expt _bb_othexp _f_bb_female _f_bb_edulvl _f_bb_age  _f_bb_workany _f_bb_workformal _f_bb_worki _f_bb_la _f_bb_expected_earn _f_bb_expt 		_f_bb_othexp treatment scell2 endline;

keep if endline == 1;


tempfile tempdata ;
save `tempdata', replace;


******* Summary stats only ;


// fix flags;


foreach var of varlist $balance{;
	
	recode `var' 0 = . if _f`var' == 1;
};


iebaltab  $balance,	grpvar(treatment)  total fixedeffect(scell) vce(robust) save("$tables_analysis/table_a07") replace;



use `tempdata',clear;


* RI for individual tests ;

local col " "ttest p-value" "RI p-value" ";
local row ""Female" "Age mean" "College" "Any past work experience" "Formal work experience" "Informal work experience" "Lowest acceptable daily wage" "Expected daily wage after grad." "Expected tuition next year" "Expected other educ "";

matrix P = J(10,2,.);
matrix rowname P = `row';
matrix colname P = `col';


local i = 1;

foreach var in $balance{;

areg `var' treatment if _f`var' == 0 , absorb(scel) robust;
testparm treatment;
	matrix P[`i',1]=`r(p)';

	ritest treatment _b[treatment], reps(`reps') seed(1230) strata(scell): areg `var' treatment if _f`var' == 0, absorb(scell) robust;
		matrix P[`i',2]=r(p);

	local ++i; 
	
};


putexcel set "$tables_analysis/`filename'.xlsx", sheet(Table A07) modify;
putexcel A1=matrix (P), names nformat(##.###) ;


#delimit ;
* Joint test ;

use `tempdata',clear;
gen _est2 = 1;

areg treatment $balance _f*,absorb(scel) robust;

testparm $balance;

testparm $balance _f*;

#delimit cr

** SUEST

ds _bb* _f_bb_edulvl _f_bb_female scell treatment _est* endline,not
loc flags "`r(varlist)'"

di "`flags'"
foreach var of varlist $balance `flags' {

		reg `var'  treatment ,absorb(scell)

		est store `var'
		}
		
suest $balance `flags'   ,vce(robust)


testparm treatment


log close 

exit 
